﻿// =====================================================
// Copyright (C) 2016-2020 Fanjia
// All rights reserved
// 机器名称: PC-LIWEI
// 创建时间: 2020/2/13 14:36:12
// 创 建 人: 李伟
// ======================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using WindowsFormsApplication1.RSA.Dto;
using FanjiaHotelPlatform.utils.Kits;
using FanJiaCommon.Kits;
using log4net.Repository.Hierarchy;
using WindowsFormsApplication1.Common;

namespace WindowsFormsApplication1.RSA
{
    public class ACardReq
    {
        static string aUrl = "https://offline-api.atomefin.com.cn/";

        public static string SyncEntInfo()
        {
            //var jsonParams = @"{
            //    'CompanyId': '569fef3c-0020-4396-9be7-382b4c7d4eeb',
            //    'UserID': 'c81a2571-c04b-4604-af3f-dd435febd5af',
            //    'CityName': '杭州',
            //    'CheckInDate': '2019-03-15',
            //    'CheckOutDate': '2019-03-16',
            //    'PageNo': 1,
            //    'PageSize': 15
            //}";
            var companyInfo = new CompanyInfo();
            companyInfo.CompanyCode = "8ab159a0-be41-49ce-a7c8-f71735536afb";//"B31842F3-242D-4859-AB83-562A2DDBF897";
            companyInfo.CompanyName = "上海东帆进出口有限公司";

            var str = JsonKit.CamelCaseSerializeObject(companyInfo);
            var rsaHelper = new RSAHelper();
            var encryStr = rsaHelper.GetEncryptedMsg(str);

            var reqDto = new RequestDto
            {
                EncryptedData = encryStr
            };

            var jsonParams = JsonKit.CamelCaseSerializeObject(reqDto);

            var requestResult = WebRequestKit.CreateJsonPostHttpRequest($"{aUrl}outer/fanjia/syncEntInfo", jsonParams, 10000, System.Text.Encoding.UTF8);
            return requestResult;
        }

        public static ResultDto<List<EmpValidError>> SyncEmployee()
        {
            var empList = new EmployeeList();
            var list = new List<Employee>();
            //list.Add(new Employee
            //{
            //    EmployeeId = "FJ001",
            //    EmployeeName = "测试员1",
            //    EmployeeNumber = "FS001",
            //    Mobile = "13801012211",
            //    Status = "1"
            //});
            list.Add(new Employee
            {
                EmployeeId = "FTH0511",
                EmployeeName = "王等",
                EmployeeNumber = "FTH0511",
                Mobile = "13564540001",
                Status = "1"
            });

            list.Add(new Employee
            {
                EmployeeId = "FTH0512",
                EmployeeName = "形审",
                EmployeeNumber = "FTH0512",
                Mobile = "13564540002",
                Status = "1"
            });

            list.Add(new Employee
            {
                EmployeeId = "FTH0513",
                EmployeeName = "张卫",
                EmployeeNumber = "FTH0513",
                Mobile = "13564540003",
                Status = "1"
            });

            //list.Add(new Employee
            //{
            //    EmployeeId = "FTH0504",
            //    EmployeeName = "奥巴马",
            //    EmployeeNumber = "FST0504",
            //    Mobile = "13564549002",
            //    Status = "1"
            //});

            //for (int i = 300; i < 311; i++)
            //{
            //    list.Add(new Employee
            //    {
            //        EmployeeId = $"FT{i:d4}",
            //        EmployeeName = $"泛测{i}",
            //        EmployeeNumber = $"FST{i:d4}",
            //        Mobile = $"1380101{i:d4}",
            //        Status = "1"
            //    });
            //}

            empList.Employees = list;

            empList.CompanyCode = "8ab159a0-be41-49ce-a7c8-f71735536afb";

            var jsonParams = JsonKit.CamelCaseSerializeObject(empList);

            var requestResult = WebRequestKit.CreateJsonPostHttpRequest(
                $"{aUrl}outer/fanjia/syncEmployee", jsonParams, 100000, System.Text.Encoding.UTF8);
            return JsonKit.DeserializeJsonToObject<ResultDto<List<EmpValidError>>>(requestResult);
        }

        public static ResultDto<PayResult> Pay()
        {
            var payReq = new PayRequest();
            payReq.CompanyCode = "A0005";
            payReq.Body = "酒店预订订单费用";
            payReq.Attach = $"hotel=m0000_d00d&date={DateTime.Now:yyyy-MM-dd}";
            payReq.BizType = "hotel";
            payReq.EmployeeId = "FT0302";
            payReq.OutTradeNo = $"H{DateTime.Now:yyyyMMddHHmmss}";
            payReq.Detail = "{\"hotel\":\"gggsad\"}";
            payReq.TotalFee = "10.21";
            payReq.Ip = GetLocalIP();

            var str = JsonKit.CamelCaseSerializeObject(payReq);

            LogHelper.WriteLog(str);

            var rsaHelper = new RSAHelper();
            var encryStr = rsaHelper.GetEncryptedMsg(str);
            //var encryStr = RasBouncyCastle.GetNonce(str);

            var reqDto = new RequestDto
            {
                EncryptedData = encryStr
            };

            var jsonParams = JsonKit.CamelCaseSerializeObject(reqDto);

            var requestResult = WebRequestKit.CreateJsonPostHttpRequest(
                $"{aUrl}outer/fanjia/trade/consume", jsonParams, 10000, System.Text.Encoding.UTF8);
            var r = JsonKit.DeserializeJsonToObject<ResultDto<string>>(requestResult);

            var rJson = RasBouncyCastle.RSADecryptByPrivateKey(r.Data);

            LogHelper.WriteLog("Pay返回结果解密：" + rJson);

            var rstData = JsonKit.DeserializeJsonToObject<PayResult>(rJson);
            var m = new ResultDto<PayResult>
            {
                Code = r.Code,
                Msg = r.Msg,
                Data = rstData
            };

            return m;
        }

        public static ResultDto<PayQueryResult> PayQuery()
        {
            var payReq = new PayQueryRequest();
            payReq.CompanyCode = "A0005";
            payReq.OutTradeNo = "H20200214133909";

            var str = JsonKit.CamelCaseSerializeObject(payReq);

            LogHelper.WriteLog(str);

            //var rsaHelper = new RSAHelper();
            //var encryStr = rsaHelper.GetEncryptedMsg(str);

            var encryStr= RasBouncyCastle.GetNonce(str);

            var reqDto = new RequestDto
            {
                EncryptedData = encryStr
            };

            var jsonParams = JsonKit.CamelCaseSerializeObject(reqDto);

            var requestResult = WebRequestKit.CreateJsonPostHttpRequest(
                $"{aUrl}outer/fanjia/trade/consumeQuery", jsonParams, 10000, System.Text.Encoding.UTF8);
            var r = JsonKit.DeserializeJsonToObject<ResultDto<string>>(requestResult);

            var rJson = RasBouncyCastle.RSADecryptByPrivateKey(r.Data);

            LogHelper.WriteLog("PayQuery返回结果解密：" + rJson);

            var rstData = JsonKit.DeserializeJsonToObject<PayQueryResult>(rJson);
            var m = new ResultDto<PayQueryResult>
            {
                Code = r.Code,
                Msg = r.Msg,
                Data = rstData
            };

            return m;
        }

        public static ResultDto<RefundResult> Refund()
        {
            var payReq = new RefundRequest();
            payReq.CompanyCode = "A0005";
            payReq.OutRefundNo = "RF-H20200219103253-01";
            payReq.EmployeeId = "FT0302";
            payReq.OutTradeNo = "H20200219103253";
            payReq.RefundFee = "20.81";

            var str = JsonKit.CamelCaseSerializeObject(payReq);

            LogHelper.WriteLog(str);

            var rsaHelper = new RSAHelper();
            var encryStr = rsaHelper.GetEncryptedMsg(str);

            var reqDto = new RequestDto
            {
                EncryptedData = encryStr
            };

            var jsonParams = JsonKit.CamelCaseSerializeObject(reqDto);

            var requestResult = WebRequestKit.CreateJsonPostHttpRequest(
                $"{aUrl}outer/fanjia/trade/refund", jsonParams, 10000, System.Text.Encoding.UTF8);
            var r = JsonKit.DeserializeJsonToObject<ResultDto<string>>(requestResult);

            var rJson = RasBouncyCastle.RSADecryptByPrivateKey(r.Data);

            LogHelper.WriteLog("Refund返回结果解密：" + rJson);

            var rstData = JsonKit.DeserializeJsonToObject<RefundResult>(rJson);
            var m = new ResultDto<RefundResult>
            {
                Code = r.Code,
                Msg = r.Msg,
                Data = rstData
            };

            return m;
        }

        public static ResultDto<RefundQueryResult> RefundQuery()
        {
            var payReq = new RefundQueryRequest();
            payReq.CompanyCode = "A0005";
            payReq.OutTradeNo = "H20200214133909";
            payReq.OutRefundNo = "RF-H20200214133909-02";

            var str = JsonKit.CamelCaseSerializeObject(payReq);

            LogHelper.WriteLog(str);

            var rsaHelper = new RSAHelper();
            //var encryStr = rsaHelper.GetEncryptedMsg(str);
            var encryStr = RasBouncyCastle.GetNonce(str);

            var reqDto = new RequestDto
            {
                EncryptedData = encryStr
            };

            var jsonParams = JsonKit.CamelCaseSerializeObject(reqDto);

            var requestResult = WebRequestKit.CreateJsonPostHttpRequest(
                $"{aUrl}outer/fanjia/trade/refundQuery", jsonParams, 10000, System.Text.Encoding.UTF8);
            var r = JsonKit.DeserializeJsonToObject<ResultDto<string>>(requestResult);

            var rJson = RasBouncyCastle.RSADecryptByPrivateKey(r.Data);

            LogHelper.WriteLog("RefundQuery返回结果解密：" + rJson);

            var rstData = JsonKit.DeserializeJsonToObject<RefundQueryResult>(rJson);
            var m = new ResultDto<RefundQueryResult>
            {
                Code = r.Code,
                Msg = r.Msg,
                Data = rstData
            };

            return m;
        }

        public static string GetLocalIP()
        {
            try
            {
                IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
                foreach (IPAddress item in IpEntry.AddressList)
                {
                    if (item.AddressFamily == AddressFamily.InterNetwork)
                    {
                        return item.ToString();
                    }
                }
                return "";
            }
            catch { return ""; }
        }
    }
}
